package com.blb.utils;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class MybatisUtils {

    private static SqlSessionFactory sqlSessionFactory = null;

    /**
     * 获取SqlSession
     *
     * @return
     * @throws IOException
     */
    public static SqlSession getSession() throws IOException {
        if (sqlSessionFactory == null) {
            String resource = "mybatis-conf.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        }
        return sqlSessionFactory.openSession();
    }

    /**
     * 关闭session
     *
     * @param session
     */
    public static void closeSession(SqlSession session) {
        session.close();
    }
}
